package org.vesselonline.mail;

import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;

public class MailChecker extends JFrame implements ChangeListener {
  /**
   * For Serializable interface, value generated by Eclipse.
   */
  private static final long serialVersionUID = -6840803843334026377L;

  public MailChecker() throws IOException, DailyBriefingException {
    // Create and set up the window
    super(DailyBriefingUtils.INSTANCE.getApplicationName());

    // Create and set up the JavaMail content panel
    JTabbedPane mailTabPane = new JTabbedPane();
    mailTabPane.addChangeListener(this);
    mailTabPane.setFont(DailyBriefingUtils.INSTANCE.getSmallFont());

    // Add MailDisplay account panels to the tabbed pane
    MailAccount acct = new MailAccount("<display_name>", "imap", true, "<server_name>", "<account_name>", 11);
    JPanel contentPanel = new MailDisplay(0, acct);
    tabPane.addTab(acct.getAccount(), contentPanel);

    // Add the panel to the window
    add(mailTabPane, BorderLayout.CENTER);

    // Create and set up the Google content panel
    JTabbedPane googleTabPane = new JTabbedPane();
    googleTabPane.addChangeListener(this);
    googleTabPane.setFont(DailyBriefingUtils.INSTANCE.getSmallFont());

    int googleTabIndex = 0;

    TaskLists taskLists = DailyBriefingUtils.INSTANCE.getGoogleTasksService().tasklists().list().execute();
    for (TaskList taskList : taskLists.getItems()) {
      contentPanel = new TasksDisplay(googleTabIndex, 97 + googleTabIndex, taskList);
      googleTabPane.addTab("Tasks", contentPanel);
      googleTabIndex++;
    }

    CalendarList calendarList = DailyBriefingUtils.INSTANCE.getGoogleCalendarService().calendarList().list().execute();
    while (calendarList.getItems() != null) {
      for (CalendarListEntry calendarListEntry : calendarList.getItems()) {
        contentPanel = new CalendarDisplay(googleTabIndex, 67 + googleTabIndex, calendarListEntry);
        googleTabPane.addTab("Calendar", contentPanel);
        googleTabIndex++;
      }
      String pageToken = calendarList.getNextPageToken();
      if (pageToken != null && ! pageToken.isEmpty()) {
        calendarList = DailyBriefingUtils.INSTANCE.getGoogleCalendarService().calendarList().list().setPageToken(pageToken).execute();
      } else {
        break;
      }
    }

    // Add the panel to the window
    add(googleTabPane, BorderLayout.SOUTH);
  }

  public void stateChanged(ChangeEvent e) { toFront(); }
  
  /** For thread safety, this method should be invoked from the event-dispatching thread. 
   * @throws DailyBriefingException 
   * @throws IOException */
  private static void createAndShowGUI() throws IOException, DailyBriefingException {
    // Make sure we have nice window decorations
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);

    MailChecker mc = new MailChecker();
    mc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mc.pack();
    mc.setVisible(true);
  }

  public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:  create and show this application's GUI
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
        createAndShowGUI();
        } catch (IOException ioe) {
          ioe.printStackTrace();
          JOptionPane.showInputDialog("The following error has occurred:", ioe.getMessage());
        } catch (DailyBriefingException dbe) {
          dbe.printStackTrace();
          JOptionPane.showInputDialog("The following error has occurred:", dbe.getMessage());
        }
      }
    });
  }
}
